// TLogParse.cpp: implementation of the TLogParse class.
//
//////////////////////////////////////////////////////////////////////

#include "TLogParse.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CLogParse::CLogParse()
{
	iStop=EFalse;
	totalCall.eventTypeUid=KLogCallEventTypeUid; //call
	totalSms.eventTypeUid=KLogShortMessageEventTypeUid; //sms
	today.HomeTime();
	todayDT=today.DateTime();
	iMessengers = new (ELeave) CArrayFixFlat<TMessenger>(5);

	// Construct list for events
	iLogEvents = new (ELeave) CArrayPtrFlat<CLogEvent>(8);
}

void CLogParse::ConstructL()
{
	//iMessengers = new (ELeave) CArrayFixFlat<TMessenger>(5);
}

CLogParse::~CLogParse()
{
iMessengers->Reset();
delete iMessengers;

if(iLogEvents)
    iLogEvents->ResetAndDestroy();
delete iLogEvents;
}

TBool CLogParse::IsInthisDay(const TTime& aLogTime)
{
	return	(todayDT.Day()==aLogTime.DateTime().Day());
}

TBool CLogParse::IsInthisWeek(const TTime& aLogTime)
{
	return (today.WeekNoInYear()==aLogTime.WeekNoInYear());
}

TBool CLogParse::IsInThisMonth(const TTime& aLogTime)
{
	return (todayDT.Month()==aLogTime.DateTime().Month());
}

void CLogParse::ParseEventL(const CLogEvent &aEvent)
{
	TTime time=aEvent.Time();
		
		
	if(IsInThisMonth(time)) //in this month
	{
	//////////////////////////////////////////////////////////////////////////
	CLogEvent* event = CLogEvent::NewL();
	CleanupStack::PushL(event); 
	event->CopyL(aEvent);
	iLogEvents->AppendL(event);
	CleanupStack::Pop(event);
	///]//////////////////////////////////////////////////////////////////////////
		
	HBufC* direction = aEvent.Direction().AllocLC();
	TBuf<64> outgoing;
	outgoing.Append(_L("Outgoing"));
	TBuf<64> Incoming;
	Incoming.Append(_L("Incoming"));

	TMessenger man;
	man.iSMS.day.count_In=0;
	man.iSMS.day.count_Out=0;
	man.iSMS.week.count_In=0;
	man.iSMS.week.count_Out=0;
	man.iSMS.month.count_In=0;
	man.iSMS.month.count_Out=0;
	man.iCall.day.count_In=0;
	man.iCall.day.count_Out=0;
	man.iCall.day.duration_In=0;
	man.iCall.day.duration_Out=0;
	man.iCall.week.count_In=0;
	man.iCall.week.count_Out=0;
	man.iCall.week.duration_In=0;
	man.iCall.week.duration_Out=0;
	man.iCall.month.count_In=0;
	man.iCall.month.count_Out=0;
	man.iCall.month.duration_In=0;
	man.iCall.month.duration_Out=0;
	HBufC* number = aEvent.Number().AllocLC(); // Number field

	if (number->Find(_L("+86")) == 0) //Find "+86"  at first
	{
		TPtr test=number->Des();
		man.iTelephoneNumber.CopyF(test.Right(number->Length()-3));
	}
	else
	man.iTelephoneNumber.CopyF(*number);
	
	CleanupStack::PopAndDestroy(number);
		
	HBufC* name = aEvent.RemoteParty().AllocLC(); // RemoteParty field
	man.iName.CopyF(*name);
	CleanupStack::PopAndDestroy(name);
	
		if(totalCall.eventTypeUid==aEvent.EventType())
		{
			if (direction->Match(outgoing)!=KErrNotFound)
			{
				man.iCall.month.count_Out=1;
				man.iCall.month.duration_Out=aEvent.Duration();

				totalCall.month.count_Out+=1;
				totalCall.month.duration_Out+=aEvent.Duration();
			}
			if (direction->Match(Incoming)!=KErrNotFound)
			{
				man.iCall.month.count_In=1;
				man.iCall.month.duration_In=aEvent.Duration();

				totalCall.month.count_In+=1;
				totalCall.month.duration_In+=aEvent.Duration();
			}
		}
		if (totalSms.eventTypeUid==aEvent.EventType())
		{
			if (direction->Match(outgoing)!=KErrNotFound) 
			{
				man.iSMS.month.count_Out=1;
				totalSms.month.count_Out+=1;
			}
			if (direction->Match(Incoming)!=KErrNotFound)
			{
				man.iSMS.month.count_In=1;
				totalSms.month.count_In+=1;
			}
		}
		
		if (IsInthisWeek(time)) 
		{
			if(totalCall.eventTypeUid==aEvent.EventType())
			{
				if (direction->Match(outgoing)!=KErrNotFound)
				{
					man.iCall.week.count_Out=1;
					man.iCall.week.duration_Out=aEvent.Duration();

					totalCall.week.count_Out+=1;
					totalCall.week.duration_Out+=aEvent.Duration();
				}
				if (direction->Match(Incoming)!=KErrNotFound)
				{
					man.iCall.week.count_In=1;
					man.iCall.week.duration_In=aEvent.Duration();

					totalCall.week.count_In+=1;
					totalCall.week.duration_In+=aEvent.Duration();
				}
			}
			if (totalSms.eventTypeUid==aEvent.EventType())
			{
				if (direction->Match(outgoing)!=KErrNotFound) 
				{
					man.iSMS.week.count_Out=1;
					totalSms.week.count_Out+=1;
				}
				if (direction->Match(Incoming)!=KErrNotFound)
				{
					man.iSMS.week.count_In=1;
					totalSms.week.count_In+=1;
				}
			}
			if (IsInthisDay(time)) 
			{
				if(totalCall.eventTypeUid==aEvent.EventType())
				{
					if (direction->Match(outgoing)!=KErrNotFound)
					{
						man.iCall.day.count_Out=1;
						man.iCall.day.duration_Out=aEvent.Duration();

						totalCall.day.count_Out+=1;
						totalCall.day.duration_Out+=aEvent.Duration();
					}
					if (direction->Match(Incoming)!=KErrNotFound)
					{
						man.iCall.day.count_In=1;
						man.iCall.day.duration_In=aEvent.Duration();

						totalCall.day.count_In+=1;
						totalCall.day.duration_In+=aEvent.Duration();
					}
				}
				if (totalSms.eventTypeUid==aEvent.EventType())
				{
					if (direction->Match(outgoing)!=KErrNotFound) 
					{
						man.iSMS.day.count_Out=1;
						totalSms.day.count_Out+=1;
					}
					if (direction->Match(Incoming)!=KErrNotFound)
					{
						man.iSMS.day.count_In=1;
						totalSms.day.count_In+=1;
					}
				}
			}
		}

	iMessengers->AppendL(man);
	//delete HBufC
	CleanupStack::PopAndDestroy(direction);
	}
	else
	{
		iStop=ETrue; //stop
	}
}


void CLogParse::ReSet()
{
	iLogEvents->ResetAndDestroy(); 
	SetStart();
	
	iMessengers->Reset();
	totalSms.day.count_In=0;
	totalSms.day.count_Out=0;
	totalSms.week.count_In=0;
	totalSms.week.count_Out=0;
	totalSms.month.count_In=0;
	totalSms.month.count_Out=0;
	totalCall.day.count_In=0;
	totalCall.day.count_Out=0;
	totalCall.day.duration_In=0;
	totalCall.day.duration_Out=0;
	totalCall.week.count_In=0;
	totalCall.week.count_Out=0;
	totalCall.week.duration_In=0;
	totalCall.week.duration_Out=0;
	totalCall.month.count_In=0;
	totalCall.month.count_Out=0;
	totalCall.month.duration_In=0;
	totalCall.month.duration_Out=0;
}


TInt CLogParse::CallAndSMSLogParse()
{
	// Sort the array, based upon the telephone numbers
	TKeyArrayFix sortKey(_FOFF(TMessenger, iTelephoneNumber), ECmpNormal);
	iMessengers->Sort(sortKey);

	//Combine and sum the entries in array
	TInt i = 0;
	TInt numberOfEntries = iMessengers->Count();

	if (!numberOfEntries) return 0;

	for (TInt j = 1; j < numberOfEntries; j++)
		{
		if ((*iMessengers)[i].iTelephoneNumber == (*iMessengers)[j].iTelephoneNumber)
			{
			(*iMessengers)[i].iCall.day.count_In += (*iMessengers)[j].iCall.day.count_In;
			(*iMessengers)[i].iCall.day.count_Out += (*iMessengers)[j].iCall.day.count_Out;
			(*iMessengers)[i].iCall.day.duration_In+= (*iMessengers)[j].iCall.day.duration_In;
			(*iMessengers)[i].iCall.day.duration_Out += (*iMessengers)[j].iCall.day.duration_Out;

			(*iMessengers)[i].iCall.week.count_In += (*iMessengers)[j].iCall.week.count_In;
			(*iMessengers)[i].iCall.week.count_Out += (*iMessengers)[j].iCall.week.count_Out;
			(*iMessengers)[i].iCall.week.duration_In+= (*iMessengers)[j].iCall.week.duration_In;
			(*iMessengers)[i].iCall.week.duration_Out += (*iMessengers)[j].iCall.week.duration_Out;

			(*iMessengers)[i].iCall.month.count_In += (*iMessengers)[j].iCall.month.count_In;
			(*iMessengers)[i].iCall.month.count_Out += (*iMessengers)[j].iCall.month.count_Out;
			(*iMessengers)[i].iCall.month.duration_In+= (*iMessengers)[j].iCall.month.duration_In;
			(*iMessengers)[i].iCall.month.duration_Out += (*iMessengers)[j].iCall.month.duration_Out;

			//SMS
			(*iMessengers)[i].iSMS.day.count_In += (*iMessengers)[j].iSMS.day.count_In;
			(*iMessengers)[i].iSMS.day.count_Out += (*iMessengers)[j].iSMS.day.count_Out;
			(*iMessengers)[i].iSMS.week.count_In += (*iMessengers)[j].iSMS.week.count_In;
			(*iMessengers)[i].iSMS.week.count_Out += (*iMessengers)[j].iSMS.week.count_Out;
			(*iMessengers)[i].iSMS.month.count_In += (*iMessengers)[j].iSMS.month.count_In;
			(*iMessengers)[i].iSMS.month.count_Out += (*iMessengers)[j].iSMS.month.count_Out;
			}
		else
			{
			i++;
			(*iMessengers)[i] = (*iMessengers)[j];
			}
		}

	// shrinking the array if the summation has reduced the number of entries
	iMessengers->ResizeL(i + 1);
	return i;
}

CLogParse* CLogParse::NewL()
{
	CLogParse* self = CLogParse::NewLC();
	CleanupStack::Pop(self);
	return self;
}

CLogParse* CLogParse::NewLC()
{
	CLogParse* self = new (ELeave) CLogParse;
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
}

TBool CLogParse::IsStop()
{
	return iStop; 
}

void CLogParse::SetStart()
{
	iStop=EFalse;
}

void CLogParse::SetStopParse()
{
	iStop=ETrue;
}
